﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using LNUGame.Model;

namespace LNUGame.View
{
    /// <summary>
    /// Class responsible for drawing everything related to the menus
    /// </summary>
    class MenuView
    {
        private SpriteBatch m_spriteBatch;
        private SpriteFont m_font;
        private Texture2D m_textureBackground;

        /// <summary>
        /// Main constructor.
        /// Loads the fonts to use into memory.
        /// </summary>
        /// <param name="a_manager">The GraphicsDeviceManager to use</param>
        /// <param name="a_contentLoader">The ContentManager to use</param>
        public MenuView(GraphicsDeviceManager a_manager, ContentManager a_contentLoader)
        {
            m_spriteBatch = new SpriteBatch(a_manager.GraphicsDevice);
            m_font = a_contentLoader.Load<SpriteFont>("fonts/menufont");
            m_textureBackground = a_contentLoader.Load<Texture2D>("textures/main_menu");
        }

        /// <summary>
        /// Draws the complete menu by iterating through all available options.
        /// Checks if the current option is selected, in which case it is drawn in a different color to the rest of the options
        /// </summary>
        /// <param name="a_manager">The GraphicsDevice to use</param>
        /// <param name="a_menuOptions">Alla available menuoptions</param>
        /// <param name="a_selectedOption">The currently selected menuoption</param>
        internal void DrawMenu(GraphicsDevice a_graphicsDevice, MenuModel.MenuOption[] a_menuOptions, MenuModel.MenuOption a_selectedOption)
        {
            a_graphicsDevice.Clear(Color.Gray);

            Vector2 viewportSize = new Vector2(a_graphicsDevice.Viewport.Width, a_graphicsDevice.Viewport.Height);

            m_spriteBatch.Begin();

            m_spriteBatch.Draw(m_textureBackground, new Vector2(0, 0), Color.White);

            foreach (MenuModel.MenuOption option in a_menuOptions)
            {
                if (option == a_selectedOption)
                {
                    DrawMenuOption(viewportSize, option, new Color(255, 222,0));
                }
                else
                {
                    DrawMenuOption(viewportSize, option, Color.White);
                }
            }

            m_spriteBatch.End();
        }

        /// <summary>
        /// Draws a single menuoption
        /// </summary>
        /// <param name="a_viewPortSize">The size of the drawable window</param>
        /// <param name="a_option">The option to be drawn</param>
        /// <param name="a_color">The color to draw the menuoption with</param>
        private void DrawMenuOption(Vector2 a_viewPortSize, MenuModel.MenuOption a_option, Color a_color)
        {
            string optionString = null;

            switch (a_option)
            {
                case MenuModel.MenuOption.START_GAME:
                    optionString = "New game";
                    break;
                case MenuModel.MenuOption.CONTROLS:
                    optionString = "Controls";
                    break;
                case MenuModel.MenuOption.EXIT_GAME:
                    optionString = "Exit game";
                    break;
                case MenuModel.MenuOption.DISPLAY_CONTROLS:
                    optionString = "Move right: Right arrow\n" +
                                   "Move left: Left arrow\n" +
                                   "Jump: Space";
                    break;
                case MenuModel.MenuOption.BACK:
                    optionString = "Back to main menu";
                    break;
            }

            // Get the width and height of the text to be shown
            Vector2 textSize = m_font.MeasureString(optionString);

            // Find and set the position
            float horizontalPosition = a_viewPortSize.X / 4 - textSize.X / 2;
            float verticalPosition = 0.0f;

            if (a_option == MenuModel.MenuOption.DISPLAY_CONTROLS)
            {
                verticalPosition = (a_viewPortSize.Y / 4.0f) + ((a_viewPortSize.Y / 6.0f) * 1 - textSize.Y / 2);
            }
            else if (a_option == MenuModel.MenuOption.BACK)
            {
                verticalPosition = (a_viewPortSize.Y / 4.0f) + ((a_viewPortSize.Y / 6.0f) * 2 - textSize.Y / 2);
            }
            else
            {
                verticalPosition = (a_viewPortSize.Y / 4.0f) + ((a_viewPortSize.Y / 6.0f) * (int)a_option - textSize.Y / 2);
            }
            Vector2 textPosition = new Vector2(horizontalPosition, verticalPosition);

            m_spriteBatch.DrawString(m_font, optionString, textPosition, a_color);
        }
    }
}
